package com.ymatou.iapi.userservice.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.login.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.LockUserRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymatou.iapi.userservice.parameter.req.BindThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.LockUserRequestDtoCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*锁定用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LockUserRequestDto{
	private static LockUserRequestDtoBean lockuserrequestdtoBean;
	private static LockUserRequestDtoCall lockuserrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("锁定用户接口");
	}

	@Before
	public void caseUp() {
		lockuserrequestdtoBean=new LockUserRequestDtoBean();
		lockuserrequestdtoCall=new LockUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lockuserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockUserRequestDto_001() {
		Logger.start(true,"锁定用户,验证用户登录提示用户已锁定");
		try {			
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(userId);
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(users);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();
//			Logger.verifyIsContains(1,lockuserrequestdtoCall.getJsonObject().get("Result").getAsJsonObject().get("LockCount").getAsInt(), "验证锁定");
			Logger.verifyEquals("true",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Userinfo ui = new JUserWapper().selectUserinfoByUid(userId).get(0);
			Logger.verifyEquals(1,ui.getUserState(), "验证UserState1锁定");
			//待确认？
			Logger.verifyIsNull(new UserTokenWapper().getUserTokenByUid(userId),"验证token被删除");
			
//			MessageListBean MessageListBean = MqQueryCallService.initUserLock(userId);
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			Logger.verifyEquals(1, joa.size(), "验证发送了一条userlock消息");
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LockUserRequestDto_002() {
		Logger.start(true,"锁定多个用户,验证用户登录提示用户已锁定");
		try {		
			Logger.comment("注册一个新用户");
			ArrayList<Integer> userIds=new ArrayList<Integer>();
			String ip=TestDataManager.getRandomIp();
			for (int i=0;i<3;i++){
				Logger.comment("注册一个新用户");
				CreateUserReq cur=new CreateUserReq();
				cur=JUserCallService.initCreateUserBean(cur);
				int userId = JUserCallService.createUserCall(cur);
				userIds.add(userId);
			}	
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(userIds);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();
//			Logger.verifyIsContains(3,lockuserrequestdtoCall.getJsonObject().get("Result").getAsJsonObject().get("LockCount").getAsInt(), "验证锁定");
			Logger.verifyEquals("true",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			
//			for (Integer userId:userIds){
//				Userinfo ui = new JUserWapper().selectUserinfoByUid(userId).get(0);
//				Logger.verifyEquals(1,ui.getUserState(), "验证UserState1锁定");
//				//待确认？
//				Logger.verifyIsNull(new UserTokenWapper().getUserTokenByUid(userId),"验证token被删除");
//			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_003() {
		Logger.start(false,"用户id为空");
		try {			
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyIsContains("用户ID未提供",lockuserrequestdtoCall.getString("LastErrorMessage"), "验证提示信息");
			Logger.verifyEquals("false",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_004() {
		Logger.start(false,"操作为空");
		try {			
			lockuserrequestdtoBean.setUserId(new ArrayList<Integer>());
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyIsContains("操作者不能为空",lockuserrequestdtoCall.getString("LastErrorMessage"), "验证提示信息");
			Logger.verifyEquals("false",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_005() {
		Logger.start(false,"操作描述为空");
		try {			
			lockuserrequestdtoBean.setUserId((ArrayList<Integer>)(new ArrayList<Integer>()));
			lockuserrequestdtoBean.setOperating("autotest");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyIsContains("解锁原因不能为空",lockuserrequestdtoCall.getString("LastErrorMessage"), "验证提示信息");
			Logger.verifyEquals("false",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockUserRequestDto_006() {
		Logger.start(true,"锁定用户,验证用户登录提示用户已锁定,token删除");
		try {			
			Logger.comment("注册一个新用户");
			WeiXinUserRequestDtoBean wxreg = JUserCallService.initWeiXinUserRequestDtoBeanForRegister();
			wxreg.setRegSource(AppSourceEnum.GlobalApp);
			Integer userId = JUserCallService.weiXinUserRequestDto(wxreg).getUid();
			UserTokenWapper ut = new UserTokenWapper();
			Logger.debug(FormatUtil.beanToGSONString(ut.getUserTokenByUserId(userId)));
			Logger.verifyEquals(1,ut.getUserTokenByUserId(userId).size(),"验证有token");
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(userId);
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(users);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();
//			Logger.verifyIsContains(1,lockuserrequestdtoCall.getJsonObject().get("Result").getAsJsonObject().get("LockCount").getAsInt(), "验证锁定");
			Logger.verifyEquals("true",lockuserrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200",lockuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Userinfo ui = new JUserWapper().selectUserinfoByUid(userId).get(0);
			Logger.verifyEquals(1,ui.getUserState(), "验证UserState1锁定");
			//待确认？
			Logger.verifyEquals(0,ut.getUserTokenByUserId(userId).size(),"验证token被删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}